package bai.publicwelfare_backend.controller.activity;

import bai.publicwelfare_backend.common.utils.JwtUtil;
import bai.publicwelfare_backend.common.utils.Result;
import bai.publicwelfare_backend.pojo.dto.activity.CharityEventDTO;
import bai.publicwelfare_backend.pojo.dto.activity.EventParticipantDTO;
import bai.publicwelfare_backend.pojo.dto.user.BadgeUser;
import bai.publicwelfare_backend.pojo.entity.activity.CharityEvent;
import bai.publicwelfare_backend.pojo.entity.activity.UserBadge;
import bai.publicwelfare_backend.service.impl.activity.ActivityService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    private static final Logger log = LogManager.getLogger(JwtUtil.class); // 修复日志记录器

    @Autowired
    private ActivityService activityService;

    //    发布
    @PostMapping("/release")
    public Result register(@RequestBody @Valid CharityEventDTO charityEventDTO, HttpServletRequest request) {
        activityService.release(charityEventDTO, request);
        return Result.success("发布成功");
    }

    //    参加
    @PostMapping("/attend")
    public Result attend(@RequestBody @Valid EventParticipantDTO dto, HttpServletRequest request) {
        activityService.attend(dto, request);
        return Result.success("参加成功");
    }

    //    获取活动
    @GetMapping("/obtain")
    public Result obtain(@RequestParam(defaultValue = "1") int pageNum,
                         @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<CharityEvent> pageInfo = activityService.obtain(pageNum, pageSize);
        return Result.success(pageInfo);
    }

    //    兑换勋章
    @PostMapping("/redeem_badge")
    public Result RedeemBadge(@RequestBody @Valid  BadgeUser badgeUser) {
        System.out.println(badgeUser);
         activityService.RedeemBadge(badgeUser);
        return Result.success("兑换成功");
    }

    //    查询勋章
    @GetMapping("/query_badge")
    public Result QueryBadge(@RequestParam Long userId) {
       List<UserBadge> badgeList =  activityService.QueryBadge(userId);
        return Result.success(badgeList);
    }

}
